package com.juzipi.demo.controller;


import com.juzipi.demo.pojo.Student;
import com.juzipi.demo.service.StudentService;
import com.juzipi.demo.tools.PageResult;
import com.juzipi.demo.tools.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("stu")
@Api("学生信息")
@CrossOrigin
public class StudentController {


    @Autowired
    private StudentService studentService;




    @RequiresRoles(logical = Logical.OR,value = {"admin","super"})
    @RequiresPermissions(logical = Logical.OR,value = {"manage","any"})
    @ApiOperation("根据id查询单个学生")
    @GetMapping("{id}")
    //这里用@PathVariable接收参数的时候 前端接口一直传不过来值
    //我觉得还是前端传值的方式有问题
    public Result queryStudentById(@RequestParam("id") Long id){
        Student student = studentService.queryStudentById(id);
        if (StringUtils.isEmpty(student)){
            return Result.fail(404,"ERROR");
        }
        return Result.success("SUCCESS",student);
    }


    /**
     * 无条件返回分页结果集
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequiresRoles(logical = Logical.OR,value = {"user","admin","super"})
    @RequiresPermissions(logical = Logical.OR,value = {"normal","manage","any"})
    @ApiOperation("学生分页结果集")
    @PostMapping("all")
    public Result queryStudentByPage(
//            @RequestParam(value = "key",required = false)String key,
            @RequestParam(value = "pageNum",defaultValue = "1")Long pageNum,//defaultValue 默认值
            @RequestParam(value = "pageSize",defaultValue = "10")Long pageSize
    ){
        PageResult<Student> pageResult = studentService.queryStudentByPage(pageSize,pageNum);
        if (CollectionUtils.isEmpty(pageResult.getItems())){
            return Result.fail(404,"查询失败");
        }
        return Result.success("查询成功",pageResult);
    }


    /**
     * 根据名字模糊查询返回分页结果集
     * @param key
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequiresRoles(logical = Logical.OR,value = {"user","admin","super"})
    @RequiresPermissions(logical = Logical.OR,value = {"normal","manage","any"})
    @ApiOperation("模糊查询分页结果集")
    @PostMapping("name")
    public Result queryStudentByName(
            @RequestParam(value = "key",required = false)String key,
            @RequestParam(value = "pageNum",defaultValue = "1")Long pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10")Long pageSize
    ){
        PageResult<Student> pageResult = studentService.queryStudentByName(key,pageNum,pageSize);
        if (CollectionUtils.isEmpty(pageResult.getItems())){
            return Result.fail(404,"查询失败");
        }
        return Result.success("查询成功",pageResult);
    }


    /**
     * 修改学生
     * @param student
     * @return
     */
    @RequiresRoles(logical = Logical.OR,value = {"admin","super"})
    @RequiresPermissions(logical = Logical.OR,value = {"manage","any"})
    @ApiOperation("更新学生信息")
    @PutMapping("update")
    public Result modifyStudent(@RequestBody Student student){
        studentService.modifyStudent(student);
        if (StringUtils.isEmpty(student)){
            return Result.fail(404,"修改失败");
        }
        return Result.successMessage("修改成功");
    }


    /**
     * 新增学生
     * @param student
     * @return
     */
    @RequiresRoles(logical = Logical.OR,value = {"user","admin","super"})
    @RequiresPermissions(logical = Logical.OR,value = {"normal","manage","any"})
    @ApiOperation("新增学生信息")
    @PostMapping("insert")
    public Result addStudent(@RequestBody Student student){
        studentService.addStudent(student);
        if (StringUtils.isEmpty(student)){
            return Result.fail(404,"新增失败");
        }
        return Result.successMessage("新增成功");
    }



    /**
     * 根据id删除学生
     * @param id
     * @return
     */
    @RequiresRoles(logical = Logical.OR,value = {"super"})
    @RequiresPermissions(logical = Logical.OR,value = {"any"})
    @ApiOperation("根据id删除学生")
    @DeleteMapping("delete")
    public Result removeStudentById(@RequestParam Long id){
        studentService.removeStudentById(id);
        if (id == null){
            return Result.fail(404,"删除失败");
        }
        return Result.successMessage("删除成功");
    }


    /**
     * 恢复删除的学生
     * @return
     */
    @RequiresRoles(logical = Logical.OR,value = {"admin","super"})
    @RequiresPermissions(logical = Logical.OR,value = {"manage","any"})
    @PostMapping("reset")
    @ApiOperation("恢复删除的学生")
    public Result resetStudentAll(){
        Integer num = studentService.resetStudentAll();
        if (num == null){
            return Result.fail(500,"ERROR");
        }
        return Result.successMessage("SUCCESS");
    }


    /**
     * 获取到的数量集合
     * @return
     */
    @RequiresRoles(logical = Logical.OR,value = {"user","admin","super"})
    @RequiresPermissions(logical = Logical.OR,value = {"normal","manage","any"})
    @GetMapping("student")
    @ApiOperation("获取所有学生的数量")
    public Result queryAllStudentCount(){
        List<Map<Object,Object>> maps =  studentService.queryAllStudentCount();
        if (maps == null){
            return Result.fail(404,"NOTFOUND");
        }
        return Result.success("SUCCESS",maps);
    }


    /**
     * 获取近七天新增学生的数量
     * @return
     */
    @RequiresRoles(logical = Logical.OR,value = {"user","admin","super"})
    @RequiresPermissions(logical = Logical.OR,value = {"normal","manage","any"})
    @GetMapping("new/data")
    @ApiOperation("获取最近七天新增学生的次数")
    public Result queryStudentNewData(){
        List<Map<Object,Object>> mapList = studentService.queryStudentNewData();
        if (mapList == null){
            return Result.fail(404,"NOTFOUND");
        }
        return Result.success("SUCCESS",mapList);
    }


    /**
     * 获取近七天删除学生的数量
     * @return
     */
    @RequiresRoles(logical = Logical.OR,value = {"user","admin","super"})
    @RequiresPermissions(logical = Logical.OR,value = {"normal","manage","any"})
    @ApiOperation("获取最近七天删除学生的次数")
    @GetMapping("remove/data")
    public Result queryStudentDeleteCount(){
        List<Map<Object,Object>> mapList = studentService.queryStudentDeleteCount();
        if (mapList == null){
            return Result.fail(404,"NOTFOUND");
        }
        return Result.success("SUCCESS",mapList);
    }


}
